home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 05 Programming / BFRTH4.DOC < prev    next >
Text File  |  2019-04-13  |  9KB  |  165 lines

  1. *hd3:Blazin' Forth Documentation,Strings,-#-
  2. *cn1;String Extensions*cn0
  3.  
  4. Overview:
  5.  
  6. There are two values associated with every string variable in Blazin' Forth. The first is the maximum allowable length of the string, which is set when you create a string variable. The second is the actual length of the string, which is set when you actually store the string in the variable. It is important to keep this in mind when using strings, since the string operators will truncate the string if you try to store more characters in the string than you have allotted room for. Of course, you don't have to use the whole space, but you can't overflow it.
  7.  
  8. STRING STRINGS
  9.  
  10. These two words are used to reserve variable space for the strings in memory. STRING is used to allocate a single string variable, while STRINGS allocates a string array. You must always tell Forth how long your string is going to be. It can be any length, within the maximum limit of 255. Example:
  11.  
  12. 10 STRING NAME$
  13.  
  14. Creates a string variable called NAME$ in the dictionary. Space is reserved for 10 characters. This variable will behave exactly like a regulation Forth variable, and leave its address on the stack. Note that the address is the address of the count byte, which allows you to use COUNT TYPE on the string to type it out.
  15.  
  16. STRINGS requires the number of strings you intend to store, as well as their maximum length. Example:
  17.  
  18. 5 20 STRINGS ARRAY$
  19.  
  20. Reserves room in the dictionary for 5 strings of 20 characters each. To access the 3rd string in ARRAY$, you simply type:
  21. *fp0
  22. 3 ARRAY$
  23.  
  24. And the address of the third 20 character string in ARRAY$ will be left on the stack. Each string in a string array is stored with its own count byte, and the address left when you access a string in a string array will be the address of the length byte for that string. Note that both STRING and STRINGS initialize the string length to zero.
  25.  
  26.  
  27. "
  28. " is used to compile string literals. When it is executed, it leaves the address of the count byte of the string on the stack. For example:
  29.  
  30. : JOHN$  " JOHN" COUNT TYPE ;
  31.  
  32. When executed, JOHN$ will print JOHN on the terminal. Note that " may also be used outside a definition. In this case, the string will be stored at PAD, and the current address of PAD will be left on the stack.
  33.  
  34. $SIZE $LENGTH
  35. These two words are used to determine the length of strings or their maximum allowable size. $SIZE will leave the maximum allowable length of the string on the stack, while $LENGTH will leave the length of the string currently stored in the variable. Example:
  36.  
  37. NAME$ $SIZE ( leaves the maximum number of characters allowable in NAME$)
  38.  
  39. NAME$ $LENGTH ( leaves the length of the string currently stored in NAME$)
  40.  
  41. $!
  42.  
  43. This word is used to store a string in a variable. You can use it to initialize a string variable from a string literal. Example:
  44.  
  45. " This is a string" NAME$ $! ( store string literal in NAME$)
  46.  
  47. Note that strings will be truncated to the max length allowable for the string variable you are moving the string too. So for example, if NAME$ was created with a maximum size of 5, the only characters actually stored in NAME$ would be "THIS ".
  48.  
  49. You can also move strings from one variable to another with this word:
  50.  
  51. TRASH$ NAME$ $! ( move the string in TRASH$ to NAME$ )
  52.  
  53. Note that this word only operates on string variables - you can't use it, for example, to move a string to the PAD (see COPY$ for a way to do this.)
  54.  
  55. $+
  56. This performs the concatenation operation. Once again, if the total length of the concatenated string exceeds the length of the variable, then the string will be truncated. Example:
  57.  
  58. FOO$ BAR$ $+ ( the concatenated string is left in FOO$ )
  59.  
  60. Strings may be added (concatenated) to themselves:
  61.  
  62. FOO$ FOO$ $+  or  FOO$ DUP $+
  63.  
  64. $?
  65. This simply types out the string. Example:
  66.  
  67. NAME$ $? ( type current string in NAME$)
  68.  
  69. COPY$
  70. This word will extract a portion of a string, and move it to another string. It takes four arguments. The string address from which the string is going to be copied, the starting character of the substring, then ending character of the substring, and the destination. Example:
  71.  
  72. NAME$ 2 4 LIST$ COPY$ ( move characters 2 thru 4 of NAME$ to the start of LIST$)
  73. You can also move a string to the pad with this word:
  74.  
  75. NAME$ 2 10 PAD  COPY$ ( move characters 2-10 of NAME$ to the PAD)
  76.  
  77. Note that you can move the entire string by simply giving indexes of 1 and 255:
  78.  
  79. NAME$ 1 255 PAD COPY$ ( move the whole string to the PAD)
  80.  
  81.  
  82. +$!
  83. This word is used to store a string in the middle of another string. For example:
  84.  
  85. NAME$ JUNK$   4  +$!
  86.  
  87. Will store NAME$ starting at the 4th character in JUNK$. This is a very useful string operation, particularly in conjuntion with COPY$ :
  88.  
  89. NAME$ 2 4 PAD COPY$    PAD JUNK$ 4 +$! 
  90.  
  91. Will move the 2 thru the 4th characters of NAME$ to JUNK$, starting at the 4th character of JUNK$ ( try this one in BASIC!).
  92.  
  93. Note that +$! will only modify an already initialized string. For example, if JUNK$ contains a 10 character string, then:
  94. NAME$ 2 4 PAD COPY$ PAD JUNK$ 11 +$!
  95.  
  96. will be a null operation. There are often cases when you want to build up an unitialized string from smaller pieces, or to use a single string variable as a one dimensional string array. To do this, you must first initialize the string to all spaces. The word $BLANK has been provided for this purpose. $BLANK will set the entire string to spaces, and then set the length of the string equal to the maximum size for that string. Example:
  97.  
  98. LIST$ $BLANK ( blank LIST$)
  99.  
  100. IN$
  101.  
  102. This word will find the first occurence of a string in another string, and return the index of the first character. If nothing is found, a 0 is returned. Example:
  103.  
  104. NAME$ LIST$ IN$ ( return the first occurence of NAME$ in LIST$)
  105.  
  106.  
  107. $=  $<  $>   $<>  $<=   $>=
  108.  
  109. These are the string boolean operators. Note that, everything else being equal, the longer string is considered the greater, and also that lower case is not the same as upper case.
  110.  
  111. >LOWER
  112.  
  113. This word is principly useful for carrying out string comparisons when you want to ignore case. It takes a starting address, and a count, and converts the entire thing to lower case. (Lower case being the same as upper case when the computer is in uppercase/graphics mode). Note that the string is converted where it stands, so if you want to preserve the original form, move it somewhere else before using this word.
  114.  
  115. Example: Check if two strings are equal:
  116. NAME$ TEST$ $=
  117.  
  118. Example: Check if two strings are equal, but ignore case:
  119.  
  120. NAME$ DUP COUNT >LOWER TEST$ DUP COUNT >LOWER $=
  121.  
  122. Example: Check if two strings are equal, ignore case, but preserve the original form of the strings:
  123.  
  124. NAME$ 1 255 PAD COPY$ ( move to pad)
  125. PAD COUNT >LOWER      ( convert to lower case)
  126. TEST$ TRASH$ $!       ( move TEST$ to a temporary variable)
  127. TRASH$ COUNT >LOWER   ( convert to lower case)
  128. PAD TRASH$ $=         ( compare)
  129.  
  130.  
  131. INPUT$
  132. This accepts input from the keyboard, and moves the string to the variable whose address is on the stack. Note that the PAD is used for temporary storage, and a copy will remain there immediately after using this word. Note also that the full length of the input will be in pad, but it will be truncated if moved to a variable which is not large enough.
  133.  
  134. Example:
  135.  
  136. : GETNAME ." Please type your name " NAME$ INPUT$ ;
  137.  
  138. This example will prompt the user, and store his response in NAME$.
  139.  
  140. VALUE$
  141. This word will convert a string stored at an address to a double number on the stack. Example:
  142.  
  143. NUMBER$ VALUE$  ( convert the string in NUMBER$ to a double number on the stack.)
  144.  
  145.  Using these basic string operations, all necessary string handling operations may be carried out. You can also easily define your own custom string handling words. For example, if you are particularly fond of BASIC's string handling procedures, it is easy to define Forth equivalents using these operators. For example:
  146.  
  147. : LEFT$   ( addr1 n addr2 --)
  148. >R 1 SWAP R> COPY$ ( move n leftmost  characters of addr1 to addr2) ;
  149.  
  150. Which you would use as follows:
  151.  
  152. NAME$ 4 TEMP$ LEFT$  ( the 4 leftmost characters in NAME$ are left in TEMP$)
  153.  
  154. : RIGHT$  ( Move n right most characters of addr1 to addr2. 
  155.             Stack:  addr1 n addr2 -- )
  156. >R OVER $LENGTH 1+ SWAP -  OVER $LENGTH R> COPY$ ;
  157.  
  158. You would use RIGHT$ as follows:
  159.  
  160. NAME$ 4 TEMP$ RIGHT$ ( the 4 rightmost characters of NAME$ are left in TEMP$.)
  161.  
  162. You can use definitions like these to easily convert BASIC programs to FORTH. Personally, I think you will find it more flexible in the long run to use indexes into the strings, rather than the less flexible BASIC type of command. That's only an opinion - many people have lived long and productive lives, and never once indexed into a string.
  163.  
  164. *lk:blazin.doc4
  165.